package com.kehutong.crm.service.impl;

import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.mvc.http.Bean;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.webservice.WebService;
import org.coraframework.orm.Pool;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.orm.jdbc.Page;
import org.coraframework.orm.jdbc.execute.entity.FindPage;
import org.coraframework.util.Objects;

import com.kehutong.crm.entity.TrackRecord;

/**
 * 跟进记录服务实现类
 * @author: liukunlong
 * @date: 2021-03-05 16:01
 */
@WebService("/crm/trackRecord")
public class TrackRecordServiceImpl {
    private static final Logger logger = LoggerFactory.getLogger(TrackRecordServiceImpl.class);

    @Inject
    private JdbcSession jdbcSession;

    /**
     * 根据条件获取跟进记录列表
     * @param jsonObject 查询对象
     * @return 跟进记录列表
     * @throws Exception 操作异常
     */
    @ReqMapping("/getTrackRecordList")
    public Page<TrackRecord> getTrackRecordList(JSONObject jsonObject) throws Exception {
        Page<TrackRecord> result = null;
        if (logger.isDebugEnabled()) {
            //logger.debug("parameter:" + jsonObject);
        }
        FindPage<TrackRecord> find = jdbcSession.findPage(TrackRecord.class)
                .eq(jsonObject, "id", "userId","mbrCode")
                .eq("deleted", 0)
                .between("createTime", jsonObject, "beginTime", "endTime")
                .like(jsonObject, "mbrName", "desc")
                .order("createTime", "DESC") ;
        Integer pageSize = jsonObject.getInteger("pageSize");
        if (pageSize == null) {
            jsonObject.put("pageSize",Short.MAX_VALUE);
        }
        find.page(jsonObject);

        result = find.exe();
        if (logger.isDebugEnabled()) {
            logger.debug("result:" + result);
        }
        return result;
    }

    /***
     * 根据跟进记录标志获取跟进记录信息
     * @param id 跟进记录标志
     * @return 跟进记录对象
     * @throws Exception 操作异常
     */
    @ReqMapping("/getTrackRecordById")
    public TrackRecord getTrackRecordById(String id) throws Exception {
        TrackRecord result = null;
        if (logger.isDebugEnabled()) {
            logger.debug("parameter:" + id);
        }
        result = jdbcSession.findById(TrackRecord.class, id);
        if (logger.isDebugEnabled()) {
            logger.debug("result:" + result);
        }
        return result;
    }


    /**
     * 保存跟进记录
     *
     * @param trackRecord 跟进记录对象
     * @return 跟进记录对象
     * @throws Exception 操作异常
     */
    @Bean(newInstance = true, copy = true)
    @ReqMapping("/saveTrackRecord")
    public TrackRecord saveTrackRecord(TrackRecord trackRecord) throws Exception {
        if (logger.isDebugEnabled()) {
            logger.debug("parameter:" + trackRecord);
        }
        jdbcSession.insert(trackRecord);
        if (logger.isDebugEnabled()) {
            logger.debug("result:" + trackRecord);
        }
        return trackRecord;
    }


    /**
     * 更新跟进记录
     *
     * @param trackRecord 跟进记录对象
     * @return 跟进记录对象
     * @throws Exception 操作异常
     */
    @Bean(copy = true)
    @ReqMapping("/updateTrackRecord")
    public TrackRecord updateTrackRecord(TrackRecord trackRecord) throws Exception {
        if (logger.isDebugEnabled()) {
            logger.debug("parameter:" + trackRecord);
        }
        jdbcSession.updateById(trackRecord);
        if (logger.isDebugEnabled()) {
            logger.debug("result:" + trackRecord);
        }
        return trackRecord;
    }


    /***
     * 根据跟进记录标志删除跟进记录信息
     * @param id 跟进记录标志
     * @return 操作结果
     * @throws Exception 操作异常
     */
    @ReqMapping("/deleteTrackRecordById")
    public Boolean deleteTrackRecordById(String id) throws Exception {
        boolean flag = false;
        if (logger.isDebugEnabled()) {
            logger.debug("parameter:" + id);
        }
        if (!Objects.isEmpty(id)) {
            TrackRecord result = Pool.get(TrackRecord.class, id);
            if (result != null) {
                jdbcSession.deleteById(TrackRecord.class,id);
                flag = true;
            }
        }
        return flag;
    }

}
